package com.sservice.service.service.impl;

import com.sservice.common.Constants;
import com.sservice.model.dao.PurchaseOrderLogMapper;
import com.sservice.model.entity.PurchaseOrderLog;
import com.sservice.urp.entity.User;
import com.sservice.model.vo.PurchaseOrderLogVo;
import com.sservice.service.service.PurchaseOrderLogService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class PurchaseOrderLogServiceImpl implements PurchaseOrderLogService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private PurchaseOrderLogMapper dataDao;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return dataDao.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(PurchaseOrderLog record) {
        return dataDao.insert(record);
    }

    @Override
    public int insertSelective(PurchaseOrderLog record) {
        return dataDao.insertSelective(record);
    }

    @Override
    public PurchaseOrderLog selectByPrimaryKey(Integer id) {
        return dataDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(PurchaseOrderLog record) {
        return dataDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PurchaseOrderLog record) {
        return dataDao.updateByPrimaryKey(record);
    }

    @Override
    public int queryByCount(PurchaseOrderLogVo record) {
        return dataDao.queryByCount(record);
    }

    @Override
    public List<PurchaseOrderLogVo> queryByPage(PurchaseOrderLogVo record) {
        return dataDao.queryByPage(record);
    }

    @Override
    public void log(Object content, HttpServletRequest request, Integer purchaseId) {
        PurchaseOrderLog log = new PurchaseOrderLog();
        if (content instanceof Map) {
            log.setContent(toString(content));
        } else {
            log.setContent(content.toString());
        }
        if (log.getContent().length() == 0) {
            return;
        }
        log.setOrderId(purchaseId.toString());
        User user = (User) request.getSession().getAttribute(Constants.USER);
        log.setOperator(String.valueOf(user.getId()));
        log.setIp(Constants.getIP(request));
        dataDao.insertSelective(log);
    }

    public String toString(Object obj) {
        List<String> strArr = new ArrayList<String>();
        Map map = (Map) obj;
        for (Object str : map.keySet()) {
            if (str.equals("checkStatus")) {
                strArr.add("审核变更：" + map.get(str));
            }
            if (str.equals("reason")) {
                strArr.add("拒绝原因：" + map.get(str));
            }
            if (str.equals("currentPrice")) {
                strArr.add("价格" + map.get(str));
            }
            if (str.equals("packSpec")) {
                strArr.add("包装规格变更：" + map.get(str));
            }
            if (str.equals("ratioSale")) {
                strArr.add("销售员佣金比例变更：" + map.get(str));
            }
            if (str.equals("ratioManger")) {
                strArr.add("销售经理佣金比例变更：" + map.get(str));
            }
            if (str.equals("ratioManger")) {
                strArr.add("销售经理佣金比例变更：" + map.get(str));
            }
            if (str.equals("remark")) {
                strArr.add("备注变更：" + map.get(str));
            }
            if (str.equals("deliverPlace")) {
                strArr.add("供货地变更：" + map.get(str));
            }
        }
        return StringUtils.join(strArr, ",");
    }
}